/*
Copyright 2007 Julian Asamer

This file is part of Fractalicious.

    Fractalicious is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Fractalicious is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Fractalicious.  If not, see <http://www.gnu.org/licenses/>.
*/

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIView.h>
#import <UIKit/UIView-Rendering.h>
#import <UIKit/UIView-Geometry.h>
#import <UIKit/UIView-Animation.h>
#import <UIKit/UITextLabel.h>
#import <UIKit/UISwitchControl.h>
#import <UIKit/UICheckbox.h>
#import <UIKit/UIImageAndTextTableCell.h>
#import <UIKit/CDStructures.h>
#import <UIKit/UIPushButton.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/UIImage.h>
#import "FOptions.h"
#import "FractalController.h"
#import "FRecalcTypes.h"
#import "FView.h"
#import "ColorChooser.h"

@implementation FOptions 

-(id)init {
	[super initWithFrame: CGRectMake(0.0f, 480.0f, 320.0f, 432.0f)];
	items = [[NSMutableArray alloc] init];
	
	options = [[UITable alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 432.0f)];

	UITableColumn *col = [[UITableColumn alloc] initWithTitle: @"MainTable" 
			identifier: @"main" width: 320.0f];
	[options addTableColumn: col]; 
	[options setDataSource: self];
	[options setDelegate: self];
	[options setRowHeight: 50];
	
	[self addSubview: options];
	
	return self;
}


- (int) numberOfRowsInTable: (UITable *)table {
	NSLog(@"rowsrequested");
    return [items count];
}
- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col {
	NSLog(@"itemrequested");
	
	return [items objectAtIndex: row];
}
- (UITableCell *) table: (UITable *)table cellForRow: (int)row column: (int)col reusing: (BOOL) reusing {
	return [items objectAtIndex: row];
}
- (void)tableRowSelected:(NSNotification*)notification {
	NSLog(@"Selected!");
	[options removeFromSuperview];
	UIView *view = [[items objectAtIndex: [options selectedRow]] view];
	
	[self setFrame: CGRectMake(0,0,320,[view frame].size.height)];
	[self addSubview: view];
	[[FractalController sharedInstance] backFromFOptions];
	selected = [[items objectAtIndex: [options selectedRow]] view];
	if ([[items objectAtIndex: [options selectedRow]] forceHidingNav]) {
		[[FractalController sharedInstance] hideBars];
	}
	
	[self bringSubviewToFront: view];
}
- (void) reloadData {
	[options reloadData];
}

- (void)backToNormal {
	if (selected != nil) {
		NSLog(@"Back to normal!");
		[selected removeFromSuperview];
		[self setFrame: CGRectMake(0.0f, 480.0f, 320.0f, 412.0f)];
		[self addSubview: options];
		selected = nil;
	}
}

- (void) show {
	[self backToNormal]; //In case an option is open
	[UIView beginAnimations:nil];
	[UIView setAnimationDuration: 0.5];
	[self setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 432.0f)];
	[UIView endAnimations];
	[self setFrame: CGRectMake(0.0f, 0.0f, 320.0f, 432.0f)];
	[self reloadData];
}

- (void)navigationBar:(UINavigationBar*)navbar buttonClicked:(int)button {
	[UIView beginAnimations:nil];
	[UIView setAnimationDuration:0.5];
	[self setFrame: CGRectMake(0.0f, 480.0f, 320.0f, 412.0f)];
	[UIView endAnimations];
	[self setFrame: CGRectMake(0.0f, 480.0f, 320.0f, 412.0f)];
	[[FractalController sharedInstance] backFromFOptions];
}


- (id)addSliderWithMin: (float)min max: (float)max value: (float) init title: (NSString *) string type: (RecalcType) d {
		FOptionsSlider * s = [[FOptionsSlider alloc] 
			initWithTitle: string rangeFrom: min to: max value: init recalcType: d];
		[s setDelegate: self];
		[items addObject: s];
		return s;
}
- (id)addCheckboxWithTitle: (NSString *)title selected: (BOOL) s type: (RecalcType) d {
	FOptionsCheckbox *cb = [[FOptionsCheckbox alloc] initWithTitle: title checked: s recalcType: d];
	[cb setDelegate: self];
	[items addObject: cb];
	return cb;
}
- (id)addColorWithColor: (FRGBColor)color title: (NSString *)title type: (RecalcType) d {
	FOptionsColor *cb = [[FOptionsColor alloc] initWithTitle: title color: color recalcType: d];
	[cb setDelegate: self];
	[items addObject: cb];
	return cb;
}
//- (void)addColorButtonWithTitle: (NSString *)title color: (NSColor*) c;


- (void) valueChanged:(RecalcType) type {
	NSLog(@"value Changed!");
	if ([delegate respondsToSelector:@selector(startRecalculation:)])
		[delegate startRecalculation: type];
}

- (void) setDelegate:(id) del {
	delegate = del;
}
- (id) delegate {
	return delegate;
}

@end
@implementation FOptionsItem
-(id)initWithRecalcType: (RecalcType) type {
	[super init];
	recalcType = type;
}
-(BOOL)forceHidingNav {
	return NO;
}

- (void)setDelegate:(id)d {
	delegate = d;
}
- (id)delegate {
	return delegate;
}

@end

@implementation FOptionsSlider
- (id)initWithTitle: (NSString *) t rangeFrom: (float) min to: (float) max value: (float) init recalcType: (RecalcType) type {
	val = init;	
	//Create cell
	[super initWithRecalcType: type];
	[self setTitle: t];
	
	//Create view
	view = [[UIView alloc] initWithFrame: CGRectMake(0,0,320, 50)];
	UITextLabel *l = [[UITextLabel alloc] initWithFrame: CGRectMake(0,5,280,20)];
	[l setText: t];
	[l setBackgroundColor: [FView colorWithRed:0 green:0 blue:0 alpha:0]];
	[l setColor: [FView colorWithRed:1 green:1 blue:1 alpha: 1]];
	[l setCentersHorizontally: YES];
	[view addSubview: l];
	sl = [[UISliderControl alloc] initWithFrame: CGRectMake(0,20,280,30)];
	[sl setMaxValue: max];
	[sl setMinValue: min];
	[sl setValue: init];
	[sl addTarget:self action:@selector(buttonUp:) forEvents:(1<<6)];
	[sl addTarget:self action:@selector(buttonDragged:) forEvents:(1<<2)];
	[sl setBackgroundColor: [FView colorWithRed:0 green:0 blue:0 alpha:0]];
	[view addSubview: sl];
	
	UIPushButton *close = [[UIPushButton alloc] initWithImage: [UIImage applicationImageNamed:@"close.png"]];
	[close setTapDelegate: self];
	[close setFrame: CGRectMake(280.0f,5.0f,40.0f,40.0f)];
	[view addSubview: close];
	[view setBackgroundColor: [FView colorWithRed:0 green:0 blue:0.2 alpha:0.7]];
	return self;
}

- (void)view:(id) view handleTapWithCount:(int) count event: (id) event fingerCount:(int) fingers {
	NSLog(@"HandleTap: %i withFingers: %i",count,fingers);
	if ([delegate respondsToSelector: @selector(backToNormal)])
		[delegate backToNormal];
}

- (float)controlValue {
	return val;
}

- (UIView *)view {
	return view;
}

- (void)buttonDragged: (GSEvent *) event {
	if (val!=[sl value]) {
		val = [sl value];		
		if ([delegate respondsToSelector:@selector(valueChanged:)]) {
			if (recalcType==Full||recalcType==Fast) {
				[delegate valueChanged: Fast]; //Let's just assume that fast recalculation is better if recalculation is needed at all.
			} else  {
				[delegate valueChanged: recalcType];
			}
		}
	}
}
- (void)buttonUp: (GSEvent *) event {
	if (val!=[sl value]) {
		val = [sl value];
		if ([delegate respondsToSelector:@selector(valueChanged:)]) 
			[delegate valueChanged: recalcType];
	}
	//NSLog(@"Slidervalue: %f", [sl value]);
}

@end


@implementation FOptionsCheckbox 
-(id)initWithTitle: (NSString *) t checked: (BOOL) sel recalcType:(RecalcType) type {
	[super initWithRecalcType: type];
	val = sel;
	
	[self setTitle: t];
	cb = [[UISwitchControl alloc] init];
	CGRect r = [cb frame];
	[cb setFrame: CGRectMake(300-r.size.width, (50-r.size.height)/2 , r.size.width, r.size.height)];
	[cb setValue: sel];
	[cb addTarget:self action:@selector(buttonUp:) forEvents:(1<<6)];
	[cb2 setBackgroundColor: [FView colorWithRed:0 green:0 blue:0 alpha:0]];
	[self addSubview: cb];
	[self setAlpha: 1.0f];
	
	
	
	//View
	view = [[UIView alloc] initWithFrame: CGRectMake(0,0,320,50)];
	
	UITextLabel *l2 = [[UITextLabel alloc] initWithFrame: CGRectMake(120,0,300-120,50)];
	[l2 setText: t];
	[l2 setBackgroundColor: [FView colorWithRed:0 green:0 blue:0.2 alpha:0]];
	[l2 setColor: [FView colorWithRed:1 green:1 blue:1 alpha: 1]];
	
	cb2 = [[UISwitchControl alloc] init]; //WithFrame: CGRectMake(10,0,320,50)];
	[cb2 setFrame: CGRectMake(10,(50-r.size.height)/2,r.size.width,r.size.height)];
	[cb2 setValue: sel];
	[cb2 addTarget:self action:@selector(buttonUp:) forEvents:(1<<6)];
	[cb2 setBackgroundColor: [FView colorWithRed:0 green:0 blue:0.2 alpha:0]];
	
	UIPushButton *close = [[UIPushButton alloc] initWithImage: [UIImage applicationImageNamed:@"close.png"]];
	[close setTapDelegate: self];
	[close setFrame: CGRectMake(280.0f,5.0f,40.0f,40.0f)];
	
	[view addSubview: cb2];
	[view addSubview: l2];
	[view addSubview: close];
	
	[view setBackgroundColor: [FView colorWithRed:0 green:0 blue:0.2 alpha:0.7]];
	
	
	return self;
}

- (void)view:(id) view handleTapWithCount:(int) count event: (id) event fingerCount:(int) fingers {
	NSLog(@"HandleTap: %i withFingers: %i",count,fingers);
	if ([delegate respondsToSelector: @selector(backToNormal)])
		[delegate backToNormal];
}

- (BOOL)controlValue {
	return val;
}

- (UIView *) view {
	return view;
}

- (void)buttonUp: (GSEvent *) event {
	if (val != [cb value] || val != [cb2 value]) {
		val=!val;
		[cb setValue: val];
		[cb2 setValue: val];
		
		if ([delegate respondsToSelector:@selector(valueChanged:)]) 
			[delegate valueChanged: recalcType];
	}
}

@end

@implementation FOptionsColor
- (id)initWithTitle: (NSString *) t color: (FRGBColor)c recalcType: (RecalcType) type {
	[super initWithRecalcType: type];
	
	[self setTitle: t];	
	cc = [[ColorChooser alloc] initWithColor: c];
	[cc setDelegate: self];
	
	return self;
}
-(BOOL)forceHidingNav {
	return YES;
}
- (void)setDelegate:(id)d {
	delegate = d;
}
- (id)delegate {
	return delegate;
}
- (UIView *)view {
	return cc;
}
- (FRGBColor) controlValue {
	return [cc color];
}
- (void)colorChosen:(FRGBColor) color tapedOk: (BOOL) ok {
	if (ok&&[delegate respondsToSelector:@selector(valueChanged:)]) 
		[delegate valueChanged: recalcType];
	if ([delegate respondsToSelector: @selector(backToNormal)])
		[delegate backToNormal];
}
@end